package com.zhiche.lisa.bms.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.dao.model.arfund.ArFund;
import com.zhiche.lisa.bms.pojo.dto.driver.DriverDTO;
import com.zhiche.lisa.bms.pojo.dto.fleet.FleetDTO;
import com.zhiche.lisa.bms.pojo.dto.util.LspBackFleetDTO;
import com.zhiche.lisa.bms.pojo.dto.util.LspBasicDTO;
import com.zhiche.lisa.bms.pojo.dto.util.LspFleetDTO;
import com.zhiche.lisa.bms.pojo.dto.util.LspInfoDTO;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: liuanshun
 * @Description:
 * @Date: Create in 15:58 2018/9/6
 */
@Component
public class LspUtil {
	/**
	 * 连接超时时间
	 */
	@Value("${lisa.socketTimeout}")
	private int socketTimeout;

	@Value("${lisa.lspm.url}")
	private String url;

	@Value("${lisa.lspm.url}")
	private String lspUrl;

	@Value("${lisa.lspm.getLspByIdUrl}")
	private String getLspByIdUrl;

	@Value("${lisa.lspm.getLspNameUrl}")
	private String getLspNameUrl;

	@Value("${lisa.lspm.getLspInfoByTenantIdURL}")
	private String getLspInfoByTenantIdURL;

	@Value("${lisa.lspm.getFleetAllUrl}")
	private String getFleetAllUrl;

	@Value("${lisa.lspm.getFleetOneUrl}")
	private String getFleetOneUrl;

	@Value("${lisa.lspm.getLspInfoAllUrl}")
	private String getLspInfoAllUrl;

	@Value("${lisa.lspm.getFleetInfoUrl}")
	private String getFleetInfoUrl;

	@Value("${lisa.lspm.queryLspByNameURL}")
	private String queryLspByNameURL;

	@Value("${lisa.lspm.queryFleetByNameURL}")
	private String queryFleetByNameURL;

    @Value("${lisa.lspm.getFleetByIdUrl}")
    private String getFleetByIdUrl;

	@Value("${lisa.lspm.queryDriverByNameURL}")
	private String queryDriverByNameURL;

	@Value("${lisa.lspm.getDriverAllURL}")
	private String getDriverAllURL;

	@Value("${lisa.lspm.queryDriverByIdURL}")
	private String queryDriverByIdURL;

	@Value("${lisa.lspm.getLspByLoginAccount}")
	private String getLspByLoginAccount;

	/**
	 * 根据id获取承运商信息
	 *
	 * @return
	 */
	public List<LspBasicDTO> getLspName(List<Long> lspIds, String token) {
		List<LspBasicDTO> lspInfo = new ArrayList<>();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(lspIds);
		String strReturn = HttpClientUtil.postJson(lspUrl + getLspNameUrl, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<List<LspBasicDTO>> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<LspBasicDTO>>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				lspInfo = restfulResponse.getData();
			}
		}
		return lspInfo;
	}

	/**
	 * @param token
	 * @return
	 */
	public LspBasicDTO getLspByLoginAccount(String token) {
		LspBasicDTO lspInfo = null;
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String strReturn = HttpClientUtil.get(lspUrl + getLspByLoginAccount, headNamePairs, null, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<LspBasicDTO> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LspBasicDTO>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				lspInfo = restfulResponse.getData();
			}
		}
		return lspInfo;
	}

	/**
	 * 根据车队类型Id获取车队类型
	 *
	 * @return
	 */
	public LspBasicDTO getLspById(Long id) {
		LspBasicDTO listFleetType = null;
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", ""));
		List<NameValuePair> params = Lists.newArrayList();
		params.add(new BasicNameValuePair("lspId", String.valueOf(id)));
		String strReturn = HttpClientUtil.get(lspUrl + getLspByIdUrl, headNamePairs, params, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<LspBasicDTO> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LspBasicDTO>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				listFleetType = restfulResponse.getData();
			}
		}
		return listFleetType;
	}

	public LspBasicDTO getLspInfoByTenantId(Long tenantId, String token) throws BaseException {
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String lspmUrl = url + getLspInfoByTenantIdURL;
		List<NameValuePair> param = Lists.newArrayList();
		param.add(new BasicNameValuePair("tenantId", tenantId.toString()));
		String strReturn = HttpClientUtil.get(lspmUrl, headNamePairs, param, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<LspBasicDTO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LspBasicDTO>>() {
			});
			return restfulResponse.getData();
		} else {
			return null;
		}
	}

	/**
	 * 批量获取车队信息
	 *
	 * @return
	 */
	public List<LspFleetDTO> getFleetAll(List<LspFleetDTO> lspFleetDTO, String token) {
		List<LspFleetDTO> lspInfo = new ArrayList<>();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(lspFleetDTO);
		String strReturn = HttpClientUtil.postJson(lspUrl + getFleetAllUrl, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<List<LspFleetDTO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<LspFleetDTO>>>() {
			});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				lspInfo = restfulResponse.getData();
			}
		}
		return lspInfo;
	}

	/**
	 * 批量获取承运商信息
	 *
	 * @return
	 */
	public List<LspInfoDTO> getLspInfoAll(List<LspInfoDTO> lspInfoDTO, String token) {
		List<LspInfoDTO> lspInfo = new ArrayList<>();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(lspInfoDTO);
		String strReturn = HttpClientUtil.postJson(lspUrl + getLspInfoAllUrl, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<List<LspInfoDTO>> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<LspInfoDTO>>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				lspInfo = restfulResponse.getData();
			}
		}
		return lspInfo;
	}

	/**
	 * 批量获取司机信息
	 *
	 * @return
	 */
	public List<DriverDTO> getDriverAll(List<DriverDTO> driverDTOs, String token) {
		List<DriverDTO> lspInfo = new ArrayList<>();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(driverDTOs);
		String strReturn = HttpClientUtil.postJson(lspUrl + getDriverAllURL, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<List<DriverDTO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<DriverDTO>>>() {
			});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				lspInfo = restfulResponse.getData();
			}
		}
		return lspInfo;
	}

	/**
	 * 根据司机名称获取司机信息
	 *
	 * @return
	 */
	public Map<Long, DriverDTO> queryDriverByName(List<ArFund> arFundDTOS, String token) {
		Map<Long, DriverDTO> map = Maps.newHashMap();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(arFundDTOS);
		String strReturn = HttpClientUtil.postJson(lspUrl + queryDriverByNameURL, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<Map<Long, DriverDTO>> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<Map<Long, DriverDTO>>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				map = restfulResponse.getData();
			}
		}
		return map;
	}

	/**
	 * 根据司机id获取司机信息
	 *
	 * @return
	 */
	public DriverDTO queryDriverById(String driverId, String token) {
		DriverDTO returnDriverDTO = new DriverDTO();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		List<NameValuePair> paramNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("driverId", driverId));
		String strReturn = HttpClientUtil.get(lspUrl + queryDriverByIdURL + "/" + driverId, headNamePairs, null, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<DriverDTO> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<DriverDTO>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				returnDriverDTO = restfulResponse.getData();
			}
		}
		return returnDriverDTO;
	}

	/**
	 * 根据车队名称获取ID
	 *
	 * @return
	 */
	public Map<Long, FleetDTO> queryFleetByName(List<ArFund> arFundDTOS, String token) {
		Map<Long, FleetDTO> map = Maps.newHashMap();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(arFundDTOS);
		String strReturn = HttpClientUtil.postJson(lspUrl + queryFleetByNameURL, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<Map<Long, FleetDTO>> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<Map<Long, FleetDTO>>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				map = restfulResponse.getData();
			}
		}
		return map;
	}

	/**
	 * 根据名称获取承运商id
	 *
	 * @return
	 */
	public Map<Long, Long> queryLspByName(List<ArFund> arFundDTOS, String token) {
		Map<Long, Long> map = Maps.newHashMap();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(arFundDTOS);
		String strReturn = HttpClientUtil.postJson(lspUrl + queryLspByNameURL, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<Map<Long, Long>> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<Map<Long, Long>>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				map = restfulResponse.getData();
			}
		}
		return map;
	}

	/**
	 * 获取拥有匹配信息的数据
	 *
	 * @param fleetDTO
	 * @param token
	 * @return
	 */
	public FleetDTO getFleetOne(FleetDTO fleetDTO, String token) {
		FleetDTO returnFleet = null;
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", token));
		String parmsJson = JSON.toJSONString(fleetDTO);
		String strReturn = HttpClientUtil.postJson(lspUrl + getFleetOneUrl, headNamePairs, parmsJson, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<FleetDTO> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<FleetDTO>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				returnFleet = restfulResponse.getData();
			}
		}
		return returnFleet;
	}

	/**
	 * 根据车队ID获取车队名称
	 *
	 * @return
	 */
	public LspFleetDTO getFleetInfo(Long fleetId) {
		LspFleetDTO fleetInfo = null;
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		String strReturn = HttpClientUtil.get(lspUrl + getFleetInfoUrl + "/" + fleetId, headNamePairs, null, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<LspFleetDTO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LspFleetDTO>>() {
			});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				fleetInfo = restfulResponse.getData();
			}
		}
		return fleetInfo;
	}
	/**
     * 根据车队ID获取车队名称
     *
     * @return
     */
    public List<LspBackFleetDTO> getFleetById(List<String> fleetIds, String token) {
        List<LspBackFleetDTO> fleetInfo = null;
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", token));
        String param = JSONObject.toJSONString(fleetIds);
        String strReturn = HttpClientUtil.postJson(lspUrl + getFleetByIdUrl, headNamePairs, param, socketTimeout);
        if (StringUtils.isNotEmpty(strReturn)) {
            RestfulResponse<List<LspBackFleetDTO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<LspBackFleetDTO>>>() {
            });
            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                fleetInfo = restfulResponse.getData();
            }
        }
        return fleetInfo;
    }
}
